package com.pansoft.openplanet.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.efounder.utils.JfResourceUtil;
import com.pansoft.openplanet.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.utilcode.util.SizeUtils;

public class TCRefreshHeader extends LinearLayout implements RefreshHeader {

    private ImageView imageView;
    private AnimationDrawable animationDrawable;

    public TCRefreshHeader(Context context) {
        super(context);
        initView(context);
    }

    public TCRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TCRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TCRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);

    }


    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        imageView = new ImageView(context);
//        setBackgroundColor(getResources().getColor(R.color.op_primary_bg_color));
//        setBackgroundColor(getResources().getColor(R.color.frame_white_background_color));
        setBackgroundColor(JfResourceUtil.getSkinColor(R.color.frame_white_background_color));

//        ViewGroup.LayoutParams params =
//                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        imageView.setLayoutParams(params);
//        addView(imageView);
        addView(imageView, SizeUtils.dp2px(40), SizeUtils.dp2px(40));
        setMinimumHeight(SizeUtils.dp2px(80));

    }

    private void setDrawable() {
        imageView.setBackground(getResources().getDrawable(R.drawable.tc_progress_drawable));

        animationDrawable = (AnimationDrawable) imageView.getBackground();

        for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
            Drawable drawable = animationDrawable.getFrame(i);
            tintDrawable(drawable, Color.parseColor("#175DA3"));
        }
    }

    //占用内存太大了，必须释放
    private void releaseDrawable() {
        imageView.setBackground(null);
        animationDrawable = null;
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int extendHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {
        if (animationDrawable == null) {
            setDrawable();
        }
        animationDrawable.start();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        animationDrawable.stop();
        releaseDrawable();
        return 100;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        // 动画是否正在运行


        switch (newState) {
            case None:
            case PullDownToRefresh:
                if (animationDrawable == null) {
                    setDrawable();
                }
                animationDrawable.start();
                break;
            case Refreshing:
                if (animationDrawable == null) {
                    setDrawable();
                }
                if (!animationDrawable.isRunning()) {
                    animationDrawable.start();
                }
                break;
            case ReleaseToRefresh:
                if (animationDrawable != null && animationDrawable.isRunning()) {
                    //停止动画播放
                    animationDrawable.stop();
                }
        }

    }

    private Drawable tintDrawable(Drawable drawable, int colors) {
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable).mutate();
        DrawableCompat.setTint(wrappedDrawable, colors);
        return wrappedDrawable;
    }
}